package com.cgs.query.monitor

import com.cgs.query.remote.netty.INettyServerManager
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/actuator")
@Api(description = "服务端监控")
open class MonitorController {

    @Autowired
    private lateinit var nettyServerManager: INettyServerManager
    @Autowired
    private lateinit var dataSourceMonitor: DataSourceMonitor
    @Autowired
    private lateinit var serverMonitor: ServerMonitor
    @Autowired
    private lateinit var projectMonitor: ProjectMonitor

    @GetMapping("/nettyServer")
    @ApiOperation(value = "netty服务端信息")
    fun nettyServerInfo() = nettyServerManager.getAllNettyServerInfo()

    @GetMapping("/nettyServer/{name}")
    @ApiOperation(value = "netty服务日志")
    fun nettyServerLog(@ApiParam(value = "netty服务编号", required = true) @PathVariable name: String) = nettyServerManager.getNettyServerLog(name)

    @GetMapping("/source/{sourceId}")
    @ApiOperation(value = "项目度量信息")
    fun sourceMetrics(@ApiParam(value = "项目名称", required = true) @PathVariable sourceId: String) =
            dataSourceMonitor.monitor(SourceMonitorObj(sourceId))


    @GetMapping("/project/{projectName}")
    @ApiOperation(value = "项目度量信息")
    fun projectMetrics(@ApiParam(value = "项目名称", required = true) @PathVariable projectName: String) =
            projectMonitor.monitor(ProjectMonitorObj(projectName))

    @DeleteMapping("/project/{projectName}")
    @ApiOperation(value = "清空项目度量信息")
    fun cleanProjectMetrics(@ApiParam(value = "项目名称", required = true) @PathVariable projectName: String) =
            projectMonitor.clean(ProjectMonitorObj(projectName))

    @GetMapping("/server")
    @ApiOperation(value = "系统度量信息")
    fun serverMetrics() = serverMonitor.monitor(ServerMonitorObj())
}